<?php
/**
 * User: i@fedax.cn
 * Time: 2018/7/27 17:45
 */

namespace app\admin\model;


class ExclusiveTheme extends DataBase
{
    protected $name = 'Exclusive_theme';
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    public function getUpdateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * @param $page_config
     * @param $map
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getDataList()
    {
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );

        $order = input('sort','exclusive_theme_id').' '.input('order','desc');
        return $this
            ->order($order)
            ->paginate($page_config);
    }


    public function exclusiveAdd()
    {
       // halt($_FILES);

        $newData['theme_url'] =oosUpload($_FILES['theme_url'],'ss-theme','oss-cn-beijing.aliyuncs.com');
        $newData['preview_url'] = oosUpload($_FILES['preview_url'],'ss-theme','oss-cn-beijing.aliyuncs.com');
        $newData['theme_name'] = input('post.theme_name');
        $newData['is_activity'] = input('post.is_activity');
        $newData['theme_msg'] = input('post.theme_msg');
        $newData['create_time'] = time();

        $res = $this->save($newData);
        if($res) return true;
        else return false;
    }

    public function themeExclusiveUserAddData()
    {
        $exclusive_theme_id = input('get.id');
        $u_id = input('get.u_id');

        $user_id = User::where('u_id','=',$u_id)->value('id');
        if(!$user_id)  return ['code'=>-100,'msg'=>'用户不存在'];

        $res = ExclusiveGive::where(['exclusive_theme_id'=>$exclusive_theme_id,'use_user_id'=>$user_id])->find();
        if($res) return ['code'=>-100,'msg'=>'用户已经拥有该款主题，请勿添加'];

        $data = [
            'exclusive_theme_id' => $exclusive_theme_id,
            'exclusive_user_id' => $user_id,
            'use_user_id' => $user_id,
            'is_give' => 2,
            'create_time' => time()
        ];

        $re = ExclusiveGive::create($data);
        if($re) return ['code'=>100,'msg'=>'添加成功'];
        else  return ['code'=>-100,'msg'=>'添加失败'];
    }

}